package com.ruoyi.redtea.dualprevent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.redtea.dualprevent.domain.entity.*;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainTroubleList;
import com.ruoyi.redtea.dualprevent.mapper.*;
import com.ruoyi.redtea.dualprevent.service.TrainTroubleRectifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lgp
 * @description 针对表【train_trouble_rectify(隐患整改，由隐患排查项上报而来，既是任务整改本体也充当流程节点“上报”)】的数据库操作Service实现
 * @createDate 2024-05-16 10:29:48
 */
@Service
public class TrainTroubleRectifyServiceImpl implements TrainTroubleRectifyService {

    @Resource
    private TrainTroubleRectifyMapper rectifyMapper;
    /**
     * 评估Mapper
     */
    @Resource
    private TrainTroubleRecytifyAssessDataMapper recytifyAssessDataMapper;
    /**
     * 作业Mapper与验收Mapper
     */
    @Resource
    private TrainTroubleRecytifyJobDataMapper recytifyJobDataMapper;
    /**
     * 整改Mapper
     */
    @Resource
    protected TrainTroubleRecytifySelfDataMapper recytifySelfDataMapper;

    /**
     * 排查项Mapper
     */
    @Resource
    protected TrainTroubleshootItemNoteMapper trainTroubleshootItemNoteMapper;


    @Override
    public List<TrainTroubleList> queryTroubleList() {
        return rectifyMapper.queryTroubleList();
    }

    @Override
    public Map<String, Object> queryStepInfoById(Integer id, Integer step) {
        //0-上报完 1-评估完 2-整改完 3-作业完 4-验收完
        HashMap<String, Object> voMap = new HashMap<>();
        switch (step) {
            case 0://上报完
                return BeanUtil.beanToMap(rectifyMapper.queryTroubleById(id), voMap, true, true);
            case 1://评估完
                TrainTroubleRecytifyAssessData assessData = recytifyAssessDataMapper.selectVoOne(Wrappers.<TrainTroubleRecytifyAssessData>lambdaQuery().eq(TrainTroubleRecytifyAssessData::getRecytifyId, id));
                return BeanUtil.beanToMap(assessData, voMap, true, true);
            case 2://整改完
                TrainTroubleRecytifySelfData recytifySelfData = recytifySelfDataMapper.selectVoOne(Wrappers.<TrainTroubleRecytifySelfData>lambdaQuery().eq(TrainTroubleRecytifySelfData::getRecytifyId, id));
                return BeanUtil.beanToMap(recytifySelfData, voMap, true, true);
            case 3, 4://作业完、验收完
                TrainTroubleRecytifyJobData recytifyJobData = recytifyJobDataMapper.selectVoOne(Wrappers.<TrainTroubleRecytifyJobData>lambdaQuery().eq(TrainTroubleRecytifyJobData::getRecytifyId, id));
                return BeanUtil.beanToMap(recytifyJobData, voMap, true, true);
        }
        return null;
    }

    @Override
    public boolean insertTroubleRectify(TrainTroubleRectify trainTroubleRectify) {
        return rectifyMapper.insert(trainTroubleRectify) == 1;
    }

    @Override
    public boolean updateTroubleRectifyById(TrainTroubleRectify trainTroubleRectify) {
        return rectifyMapper.updateById(trainTroubleRectify) == 1;
    }

    @Override
    public boolean insertOrUpdateRectifyProcess(Integer flowStep, Object bean, boolean addOrUpdate) {
        //对应记录表更新  本体更新
        boolean result = false;
        Integer recytifyId = null;
        String nextPerson = null;
        //0-上报完 1-评估完 2-整改完 3-作业完 4-验收完

        //插入时不动，更新时-1，保持执行逻辑统一；与+-1具体由执行逻辑决定
        int step = addOrUpdate ? flowStep : flowStep--;
        switch (step) {
            case 0://待评估
                TrainTroubleRecytifyAssessData assessData = BeanUtil.toBean(bean, TrainTroubleRecytifyAssessData.class);
                recytifyId = assessData.getRecytifyId();
                nextPerson = assessData.getNextPerson();//此处应该从head中取当前请求人，分别赋值给流程实体类与整改任务实体类
                recytifyAssessDataMapper.setDelFlag(assessData);
                result = recytifyAssessDataMapper.insertOrUpdate(assessData);
                break;
            case 1://待整改
                TrainTroubleRecytifySelfData recytifySelfData = BeanUtil.toBean(bean, TrainTroubleRecytifySelfData.class);
                recytifyId = recytifySelfData.getRecytifyId();
                nextPerson = recytifySelfData.getNextPerson();
                recytifySelfDataMapper.setDelFlag(recytifySelfData);
                result = recytifySelfDataMapper.insertOrUpdate(recytifySelfData);
                break;
            case 2://待作业
                TrainTroubleRecytifyJobData recytifyJobData = BeanUtil.toBean(bean, TrainTroubleRecytifyJobData.class);
                recytifyId = recytifyJobData.getRecytifyId();
                nextPerson = recytifyJobData.getNextPerson();
                recytifyJobData.setProcesstype(1);
                recytifyJobDataMapper.setDelFlag(recytifyJobData);
                result = recytifyJobDataMapper.insertOrUpdate(recytifyJobData);
                break;
            case 3://待验收
                TrainTroubleRecytifyJobData recytifyAcceptData = BeanUtil.toBean(bean, TrainTroubleRecytifyJobData.class);
                recytifyId = recytifyAcceptData.getRecytifyId();
                nextPerson = recytifyAcceptData.getNextPerson();
                recytifyAcceptData.setProcesstype(2);
                recytifyJobDataMapper.setDelFlag(recytifyAcceptData);
                result = recytifyJobDataMapper.insertOrUpdate(recytifyAcceptData);
                break;
        }
        //更新整改任务进度
        TrainTroubleRectify trainTroubleRectify = new TrainTroubleRectify();
        trainTroubleRectify.setId(recytifyId);

        flowStep = addOrUpdate ? flowStep++ : flowStep;
        trainTroubleRectify.setProcessStep(flowStep);
        trainTroubleRectify.setNextPerson(nextPerson);
        boolean rectify = updateTroubleRectifyById(trainTroubleRectify);

        if (result && rectify) {
            return true;
        } else {
            throw new ServiceException("操作失败");
        }
    }
}




